package com.xiaoshuidi.cloud.module.rooms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentcategory.vo.*;
import com.xiaoshuidi.cloud.module.rooms.convert.equipmentcategory.EquipmentCategoryConvert;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentCategoryMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentCategory;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomEquipmentRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentCategoryRepository;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomEquipmentCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Link
 * @CreateTime: 2023-05-18 11:45
 * @Version: 1.0
 */
@Slf4j
@Service
public class RoomEquipmentCategoryServiceImpl extends ServiceImpl<EquipmentCategoryMapper, EquipmentCategory> implements IRoomEquipmentCategoryService {

    @Resource
    private IEquipmentCategoryRepository iEquipmentCategoryRepository;
    @Resource
    private RoomEquipmentRepository roomEquipmentRepository;

    @Override
    public PageResult<EquipmentCategoryPageResp> pageQuery(EquipmentCategoryPageReqVO reqVO) {
        IPage<EquipmentCategoryPageResp> list = iEquipmentCategoryRepository.pageQuery(reqVO);
        PageResult<EquipmentCategoryPageResp> page = PageResult.empty();
        page.setTotal(list.getTotal());
        page.setList(list.getRecords());
        return page;
    }

    @Override
    public Long createEquipmentCategory(EquipmentCategoryCreateReqVo createReqVO) {

        //判断是否名称已存在
        iEquipmentCategoryRepository.validateName(createReqVO.getName());
        //上级分类 默认为0
        if (createReqVO.getPcategoryId() == null) {
            createReqVO.setPcategoryId(0L);
        }
        // 插入
        EquipmentCategory equipmentCategory = EquipmentCategoryConvert.INSTANCE.convert(createReqVO);

        iEquipmentCategoryRepository.save(equipmentCategory);
        // 返回
        return equipmentCategory.getId();
    }

    @Override
    public void updateEquipmentCategory(EquipmentCategoryUpdateReqVo updateReqVO) {
        if (!updateReqVO.getOldName().equals(updateReqVO.getName())) {
            //判断是否名称已存在
            iEquipmentCategoryRepository.validateName(updateReqVO.getName());
        }
        // 更新
        EquipmentCategory updateObj = EquipmentCategoryConvert.INSTANCE.convert(updateReqVO);
        iEquipmentCategoryRepository.updateById(updateObj);
    }

    @Override
    public EquipmentCategory getEquipmentCategory(Long id) {
        return iEquipmentCategoryRepository.getById(id);
    }

    @Override
    public void deleteEquipmentCategory(Long id) {
        // 校验存在
        validateEquipmentCategoryExists(id);
        // 删除
        iEquipmentCategoryRepository.removeById(id);
    }

    @Override
    public List<EquipmentCategoryTreeResp> getTreeList(Long parentId) {
        List<EquipmentCategory> listdo = iEquipmentCategoryRepository.getTreeList(parentId);
        List<EquipmentCategoryTreeResp> list = listdo.stream().map(t -> {
            EquipmentCategoryTreeResp equipmentCategoryTreeResp
                    = new EquipmentCategoryTreeResp(t.getId(), t.getName(), t.getPcategoryId(), t.getCode(), t.getUnit());
            return equipmentCategoryTreeResp;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<Map<String, Object>> getZcList() {
        return iEquipmentCategoryRepository.getZcList();
    }

    @Override
    public List<EquipmentCategoryPageResp> listRecords() {
        List<EquipmentCategory> list = iEquipmentCategoryRepository.list();
        return list.stream().map(e -> {
            EquipmentCategoryPageResp equipmentCategoryPageResp = new EquipmentCategoryPageResp();
            BeanUtils.copyProperties(e, equipmentCategoryPageResp);
            return equipmentCategoryPageResp;
        }).collect(Collectors.toList());
    }

    private void validateEquipmentCategoryExists(Long id) {
        if (iEquipmentCategoryRepository.getById(id) == null) {
            log.error("资产类别不存在:id=" + id);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "资产类别不存在:id=" + id);
        }
        Long count = roomEquipmentRepository.countByCategory(id);
        if (count > 0) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "该资产类别下存在资产,不可删除");
        }
        Long count1 = iEquipmentCategoryRepository.countByParentId(id);
        if (count1 > 0) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "该资产类别下存在子级,不可删除");
        }
    }
}
